package com.chinaunicom.common.result;



import com.chinaunicom.common.exception.ResponseCode;

import java.io.Serializable;

/**
 * Created by wuyongchong on 2019/8/19.
 */
@SuppressWarnings("unchecked")
public class JsonResult<T> implements Serializable {

  private static final long serialVersionUID = 2652608027682835212L;

  /**
   * 错误编号
   */
  private String errorCode = "";

  /**
   * 信息(如果发生错误，那么代表错误信息)
   */
  private String message = "";

  /**
   * 返回给客户端的数据对象
   */
  private T data;

  public JsonResult() {
  }

  public JsonResult(String errorCode) {
    this.errorCode = errorCode;
  }

  public JsonResult(String errorCode, String message) {
    this.errorCode = errorCode;
    this.message = message;
  }

  public JsonResult(String errorCode, T data) {
    this.errorCode = errorCode;
    this.data = data;
  }

  public JsonResult(String errorCode, String message, T data) {
    this.errorCode = errorCode;
    this.message = message;
    this.data = data;
  }

  public String getErrorCode() {
    return errorCode;
  }

  public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public static <T> JsonResult<T> create() {
    return new JsonResult<T>();
  }

  public static <T> JsonResult<T> create(String errorCode) {
    return new JsonResult<T>(errorCode);
  }

  public static <T> JsonResult<T> create(String errorCode, String message) {
    return new JsonResult<T>(errorCode, message);
  }

  public static <T> JsonResult<T> create(String errorCode, T data) {
    return new JsonResult<T>(errorCode, data);
  }

  public static <T> JsonResult<T> create(String errorCode, String message, T data) {
    return new JsonResult<T>(errorCode, message, data);
  }

  public static <T> JsonResult<T> ok() {
    return new JsonResult<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
  }

  public static <T> JsonResult<T> success(T data) {
    return new JsonResult<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
  }

  public static <T> JsonResult<T> error(String errorCode, String message) {
    return new JsonResult<T>(errorCode, message);
  }

  public static <T> JsonResult<T> error(ResponseCode errorCode) {
    return new JsonResult<T>(errorCode.getCode(), errorCode.getMessage());
  }

  public <T> JsonResult<T> message(String message) {
    return new JsonResult(this.errorCode, message, this.data);
  }

  public <T> JsonResult<T> body(T data) {
    return new JsonResult<T>(this.errorCode, this.message, data);
  }

}